/**
************************************************************
* @file         hal_LED.c
* @brief        LED驱动
* 
* @author       Gizwits
* @date         2016-09-05
* @version      V03010201
* @copyright    Gizwits
* 
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/
#include "driver/hal_LED.h"
#include "config.h"

extern system_info_t systemInfo;
gdata_t led_data; 

COLOR_t color_arr[] = 
{ 
    /* the test val for red*/
    {       120     ,       0       ,       0      , 0      }, // 0
    {       120     ,       30      ,       0      ,0       },
    {       120     ,       60      ,       0       ,0     },
    {       120     ,       90     ,       0       ,0      },
    {       120     ,       120     ,       0       ,0      },
    {       120     ,       150    ,       0       ,0      },
    {       120     ,       180     ,       0       ,0    },
    {       120     ,       210     ,       0       ,0   },  // 7
    
    {       120       ,       255    ,       0       ,0   },   // 8
    {       110       ,       255    ,       0       ,0   },  //9
    {       100       ,       255    ,	    0	    ,0   },
    {       150       ,       255    ,       0       ,0   },
    {       120       ,       255    ,       0       ,0   },
    {       90       ,       255    ,	    0	    ,0   },
    {       60       ,       255    ,       0       ,0   },
    {       30       ,       255    ,	    0	    ,0   },// 15
    {        0        ,       255    ,	    0	    ,0   },    // 16
     

    {       0       ,       255      ,       30     ,0      }, 
    {       0       ,       255      ,       60     ,0      },
    {       0       ,       255      ,       90     ,0      },
    {       0       ,       255      ,       120     ,0      },    
    {       0       ,       255      ,       150     ,0      },
    {       0       ,       255      ,       180     ,0      },
    {       0       ,       255      ,       210     ,0      },//23


    {       0       ,       255      ,       255     ,0      }, // 24
    {      0       ,       210     ,       255     ,0      },
    {      0       ,       180   ,       255     ,0      },
    {      0       ,       150     ,       255     ,0      },
    {      0       ,       120     ,       255     ,0      },
    {      0       ,       90     ,       255     ,0      },
    {      0       ,       60      ,       255     ,0      },
    {      0       ,       30       ,       255     ,0      },//31
    {      0       ,       0       ,       255     ,0      },

    {      30       ,       0       ,       255     ,0      },
    {      60       ,       0       ,       255     ,0      },
    {      90       ,       0       ,       255     ,0      },
    {      120       ,       0       ,     255     ,0      },
    {      120      ,       0       ,      255     ,0      },
    {      120       ,       0       ,     255     ,0      },
    {      120       ,       0       ,     255     ,0      },//  39
     {	     120     ,	     0	   ,	   255	     ,0   },
     {	     120     ,	     0	   ,	   210	    ,0	  },

     {	     120     ,	     0	   ,	   180	    ,0	    },
     {	     120     ,	     0	   ,	   150	   ,0	   },
     {	     120     ,	     0	   ,	   120	    ,0	    },
     {	     120     ,	     0	   ,	   90	    ,0	    },
     {	     120     ,	     0	   ,	   60	   ,0	    },
     {	     120     ,	     0	   ,	   30	   ,  0	  },   // 47
       
};

static COLOR_t color_temp_arr[10] = {
{       120     ,       65     ,        0     ,       0       },
{       120     ,       65  ,            0      ,     0      },
{       120     ,       65      ,       0     ,       0       },
{       120     ,       60       ,       0     ,       0       },
{       120     ,       65      ,       0     ,       0     },
{       120     ,       65 ,       0     ,       5     },
{       120     ,       65       ,       0     ,       20     },
{       120     ,       65      ,       0     ,       40     },
{       120     ,       65      ,      0      ,       80    },
{       0,      0       ,          0    ,       120    }
};

static unsigned int color_7_mode_speed[] = {500,  1000,  2000, 2500, 3000,  3500, 4000, 4500, 5000, 5500};

void ICACHE_FLASH_ATTR LED_Color_Bright(unsigned char color_B)
{
    float bright = 0;
    float temp = 0;
    unsigned char tmp_red;
    unsigned char tmp_green;
    unsigned char tmp_blue;
    unsigned char tmp_white; 
    
    if(color_B >= 10 && color_B <= 100) 
    {
        led_data.COLOR_BRIGHT = color_B; 
        bright = (float)led_data.COLOR_BRIGHT / 100.0;

        temp = bright * (float)led_data.RED;
        tmp_red = (unsigned char)temp;
        temp = bright * (float)led_data.GREEN;
        tmp_green = (unsigned char)temp;
        temp = bright * (float)led_data.BLUE;
        tmp_blue = (unsigned char)temp;
        temp = bright * (float)led_data.WHITE;
        tmp_white = (unsigned char)temp;

        os_printf("N0_2 R,G,B,W : %d,%d,%d,%d \n", tmp_red, tmp_green, tmp_blue, tmp_white); 
        ledColor(tmp_red, tmp_green, tmp_blue, tmp_white); 
	
	systemInfo.devStatus.r = led_data.RED;
        systemInfo.devStatus.g = led_data.GREEN;
        systemInfo.devStatus.b = led_data.BLUE;
        systemInfo.devStatus.w = led_data.WHITE;
	systemInfo.devStatus.bright = led_data.COLOR_BRIGHT;
    	configWriteParam();
    }
}

void ICACHE_FLASH_ATTR LED_Color_Value(unsigned char color_v)
{ 
    float bright = 0;
    float temp = 0;
    unsigned char tmp_red;
    unsigned char tmp_green;
    unsigned char tmp_blue;
    unsigned char tmp_white;
    unsigned char color_value = color_v; 

    if (color_value == 48)
	color_value = 0;

    if((0 <= color_value) && (color_value < 48))
    {
        os_printf("0 <= color_value < 48 \n"); 
        os_printf("we get in =%d\r\n", color_value);
        led_data.COLOR_VAL = color_v; 
        led_data.RED = color_arr[led_data.COLOR_VAL].R;
        led_data.GREEN = color_arr[led_data.COLOR_VAL].G;
        led_data.BLUE = color_arr[led_data.COLOR_VAL].B;

        led_data.WHITE = color_arr[led_data.COLOR_VAL].W;
        os_printf("N0_1 R,G,B : %d,%d,%d \n", led_data.RED, led_data.GREEN, led_data.BLUE, led_data.WHITE); 
        bright = (float)systemInfo.devStatus.bright / 100.0;
        os_printf("bright = %d \n", bright); 
        temp = bright * (float)led_data.RED;
        os_printf("temp = %d \n", temp); 
        tmp_red = (unsigned char)temp;
        os_printf("tmp_red = %d \n", tmp_red); 
        temp = bright * (float)led_data.GREEN;
        os_printf("temp = %d \n", temp); 
        tmp_green = (unsigned char)temp;
        temp = bright * (float)led_data.BLUE;
        tmp_blue = (unsigned char)temp;
        os_printf("N0_2 R,G,B : %d,%d,%d \n", tmp_red, tmp_green, tmp_blue); 
        ledColor(tmp_red, tmp_green, tmp_blue, 0);
        led_data.RED = tmp_red;
        led_data.GREEN = tmp_green;
        led_data.BLUE = tmp_blue;
    }
    else if(color_value >= 200 && color_value < 210)
    {
        os_printf("color_value >= 200 && color_value < 210 \n"); 
        led_data.COLOR_VAL = color_v; 
        //bright = 1;//(float)led_data.COLOR_BRIGHT / 100.0;
        tmp_red = color_temp_arr[color_value - 200].R;
        //tmp_red= (unsigned char )temp;
        tmp_green = color_temp_arr[color_value - 200].G;

        tmp_blue = color_temp_arr[color_value - 200].B;
        os_printf("R,G,B,W : %d,%d,%d,%d \n", tmp_red, tmp_green, tmp_blue, color_temp_arr[color_value - 200].W); 
        ledColor(tmp_red, tmp_green, tmp_blue, color_temp_arr[color_value - 200].W);

        led_data.RED = tmp_red;
        led_data.GREEN = tmp_green;
        led_data.BLUE = tmp_blue;
        led_data.WHITE = color_temp_arr[color_value - 200].W;
    }
  
    systemInfo.devStatus.colorvalue = color_value;
    systemInfo.devStatus.r = led_data.RED;
    systemInfo.devStatus.g = led_data.GREEN;
    systemInfo.devStatus.b = led_data.BLUE;
    systemInfo.devStatus.w = led_data.WHITE;
    configWriteParam(); 
}




void ICACHE_FLASH_ATTR led_init()
{

}


void ICACHE_FLASH_ATTR ledColor(unsigned char R, unsigned char G, unsigned char B, unsigned char W)
{
     pen_color2(R,G,B,W);
}

